// src/stores/settings.ts
import { defineStore } from 'pinia';
import defaultSettings from '@/settings';
import { useDynamicTitle } from '@/utils/dynamicTitle';

interface LayoutSetting {
  theme?: string;
  sideTheme?: string;
  topNav?: boolean;
  tagsView?: boolean;
  fixedHeader?: boolean;
  sidebarLogo?: boolean;
  dynamicTitle?: boolean;
}

const { sideTheme, showSettings, topNav, tagsView, fixedHeader, sidebarLogo, dynamicTitle } = defaultSettings;

const storageSetting: LayoutSetting | null = JSON.parse(localStorage.getItem('layout-setting') || '{}') || {};

interface State {
  title: string;
  theme: string;
  sideTheme: string;
  showSettings: boolean;
  topNav: boolean;
  tagsView: boolean;
  fixedHeader: boolean;
  sidebarLogo: boolean;
  dynamicTitle: boolean;
}

const useSettingsStore = defineStore(
  'settings',
  {
    state: (): State => ({
      title: '',
      theme: storageSetting?.theme || '#409EFF',
      sideTheme: storageSetting?.sideTheme || sideTheme,
      showSettings: showSettings,
      topNav: storageSetting?.topNav === undefined ? topNav : storageSetting.topNav,
      tagsView: storageSetting?.tagsView === undefined ? tagsView : storageSetting.tagsView,
      fixedHeader: storageSetting?.fixedHeader === undefined ? fixedHeader : storageSetting.fixedHeader,
      sidebarLogo: storageSetting?.sidebarLogo === undefined ? sidebarLogo : storageSetting.sidebarLogo,
      dynamicTitle: storageSetting?.dynamicTitle === undefined ? dynamicTitle : storageSetting.dynamicTitle
    }),
    actions: {
      // 修改布局设置
      changeSetting(data: { key: keyof State; value: any }) {
        const { key, value } = data;
        if (this.hasOwnProperty(key)) {
          //this[key] = value;
          (this as any)[key] = value; // 使用类型断言绕过类型检查
        }
      },
      // 设置网页标题
      setTitle(title: string) {
        this.title = title;
        useDynamicTitle();
      }
    }
  });

export {useSettingsStore};